package com.zlx.exchange.goods.service.impl;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zlx.exchange.domain.entity.Goods;
import com.zlx.exchange.goods.dao.GoodsDao;
import com.zlx.exchange.goods.service.GoodsService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import com.github.pagehelper.PageHelper;

import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (Goods)表服务实现类
 *
 * @author makejava
 * @since 2023-09-04 19:23:37
 */
@Transactional
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;


    /**
     * 通过ID查询单条数据
     *
     * @param goodsId 主键
     * @return 返回体
     */
    @Override
    public ResponseEntity<?> queryById(Integer goodsId) {
        Goods goods = this.goodsDao.selectById(goodsId);
        return ResponseEntity.ok(goods);
    }

    /**
     * 分页查询
     *
     * @param goods    筛选条件
     * @param pageNum  分页页数
     * @param pageSize 分页总页数
     * @return 查询结果
     */
    @Override
    public ResponseEntity<?> queryByPage(Goods goods, Integer pageNum, Integer pageSize, Date timeBefore, Date timeAfter) {
        PageHelper.startPage(pageNum, pageSize);
       return ResponseEntity.ok(new PageInfo<>(this.goodsDao.selectList(new LambdaQueryWrapper<Goods>().like(StringUtils.isNotBlank(goods.getGoodsName()), Goods::getGoodsName, goods.getGoodsName())
                .like(StringUtils.isNotBlank(goods.getGoodsIntroduce()), Goods::getGoodsIntroduce, goods.getGoodsIntroduce()
                ).between(ObjectUtils.isNotNull(timeAfter, timeBefore), Goods::getCreateTime, timeAfter, timeBefore))));
    }

    /**
     * 新增数据
     *
     * @param goods 实例对象
     * @return 返回体
     */
    @Override
    public ResponseEntity<?> insert(Goods goods) {
        this.goodsDao.insert(goods);
        return ResponseEntity.ok("插入一条数据，请加入返回体");
    }

    /**
     * 修改数据
     *
     * @param goods 实例对象
     * @return 返回体
     */
    @Override
    public ResponseEntity<?> update(Goods goods) {
        this.goodsDao.updateById(goods);
        this.queryById(goods.getGoodsId());
        return ResponseEntity.ok("修改一条数据，请添加返回体");
    }

    /**
     * 通过主键删除数据
     *
     * @param goodsId 主键
     * @return 返回体，判断是否成功
     */
    @Override
    public ResponseEntity<?> deleteById(Integer goodsId) {
        boolean b = this.goodsDao.deleteById(goodsId) > 0;
        return ResponseEntity.ok("通过id删除一条数据，请加入返回体");
    }
    /**
     *批量添加或者批量修改
     * @param list List<Goods> 实例对象列表
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
    /**
     *批量删除
     * @param list List<Goods> 实例对象列表
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
}
